package com.test.service.impl;

import com.test.service.SimplePropagationTestService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class SimplePropagationTestServiceImpl implements SimplePropagationTestService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional
    public void required() {
        System.out.println("SimplePropagationTest @required");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('required SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public void supports() {
        System.out.println("SimplePropagationTest @supports");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('supports SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.MANDATORY)
    public void mandatory() {
        System.out.println("SimplePropagationTest @mandatory");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('mandatory SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNew() {
        System.out.println("SimplePropagationTest @requiresNew");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('requiresNew SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void requiresNewWithException() {
        System.out.println("SimplePropagationTest @requiresNewWithException");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('requiresNewWithException SimplePropagationTest')");
        throw new RuntimeException("requiresNewWithException SimplePropagationTest");
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void notSupported() {
        System.out.println("SimplePropagationTest @notSupported");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('notSupported SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.NEVER)
    public void never() {
        System.out.println("SimplePropagationTest @never");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('never SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void nested() {
        System.out.println("SimplePropagationTest @nested");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('nested SimplePropagationTest')");
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void nestedWithException() {
        System.out.println("SimplePropagationTest @nested");
        jdbcTemplate.execute("INSERT INTO test_table (name) VALUES ('nested SimplePropagationTest')");
        throw new RuntimeException("nestedWithException SimplePropagationTest");
    }

}
